package org.naxx.security.bootstrap;

import org.apache.log4j.Logger;
import org.springframework.util.Log4jConfigurer;

public final class Bootstrap {
	
	private static final Logger logger = Logger.getLogger(Bootstrap.class);

	public static void main(String[] args){
		try {
			Log4jConfigurer.initLogging("file:conf/log4j.properties");
			Runtime.getRuntime().addShutdownHook(new NaxxShutdownHook());
			Thread.currentThread().setName("main-naxx-thread");
			while (true) {
				try {
					Thread.sleep(Long.MAX_VALUE);
				} catch (InterruptedException e) {
					logger.info("Main naxx thread interrupted, shutting down", e);
					break;
				}
			}
			System.exit(0);
		} catch (Throwable t) {
			logger.fatal("Fatal exception while trying to start Naxx", t);
			System.err.println("Fatal exception while traying to start Naxx");
			t.printStackTrace();
			System.exit(13);
		}
	}
	
	private static class NaxxShutdownHook extends Thread {
		public void run() {
			logger.info("Naxx caught shutdown hook, finalizing Naxx...");
			// add here destruction process etc...
		}
	}
	
	private Bootstrap() {
		throw new IllegalStateException("Can not instantiate class '" 
				+ Bootstrap.class.getCanonicalName() + "'.");
	}
}
